﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;

namespace PRI_SliverlightSS
{
    public class AlbumInfo
    {
        public string ID = string.Empty;
        public string Title = string.Empty;
        public string ContentURL = string.Empty;
        public string ThumbnailURL = string.Empty;
        public string PublishedDate = string.Empty;
        public int AlbumCount = -1;
    }

    public class AuthorInfo
    {
        public string Name = string.Empty;
        public string URL = string.Empty;
        public string LastUpdated = string.Empty;

        public void Clear()
        {
            Name = "";
            URL = "";
        }
    }
    
    public class SSError
    {
        public enum eErrorCodes
        {
            NoError = 0,
            SomeError = 1,
            UnknownError = 2,
        }

        public eErrorCodes ErrorCode = eErrorCodes.NoError;
        public string Message = "NoError";
        public void Clear()
        {
            ErrorCode = eErrorCodes.NoError;
            Message = "NoError";
        }
    }

    public interface IAlbums
    {
        void SetAuthorInfo(string _name, string _url, string _last_updated);
        void AddAlbum(string _id, string _title, string _content_url, string _thumbnail_url, string _published_date);
        void Clear();

        AuthorInfo _AuthorInfo { get; }
        IList<AlbumInfo> _Albums { get; }
        PRI_Slides _CurrentSlides { get; }
        SSError _LastError { get; set; }
        string ProxyURL { get; set; }

        void FireEvent_AlbumsReady();
        void FireEvent_SlidesReady();
    }

    public enum AlbumType
    {
        AT_Picasa = 0,
        AT_Flickr = 2,
        AT_Local = 3,
    }

    public class PRI_Albums : IAlbums
    {
        private AuthorInfo m_AuthorInfo = null;     //Albums Info
        private IList<AlbumInfo> m_Albums = null;   //Albums
        private PRI_Slides m_CurrentSlides = null;  //Current Album
        private SSError m_SSError = null;
        public event Action Event_AlbumsReady = null;
        public event Action Event_SlidesReady = null;
        protected AlbumType m_AlbumType = AlbumType.AT_Local;
        private string m_strProxyURL = "http://www.priyakant.com/ws/px.aspx?url={0}";
        public PRI_Albums()
        {
            m_AuthorInfo = new AuthorInfo();
            m_Albums = new List<AlbumInfo>();
            m_CurrentSlides = new PRI_Slides();
            m_SSError = new SSError();
        }

        public string ProxyURL 
        {
            get
            {
                return m_strProxyURL;
            }
            set
            {
                string strTempURL = value.Trim();
                if (strTempURL.Length > 0)
                {
                    m_strProxyURL = strTempURL;
                }
            }
        }

        public AlbumType _AlbumType
        {
            get
            {
                return m_AlbumType;
            }
        }


        public void SetAuthorInfo(string _name, string _url, string _last_updated)
        {
            m_AuthorInfo.Name = _name;
            m_AuthorInfo.URL = _url;
            m_AuthorInfo.LastUpdated = _last_updated;
        }

        public void AddAlbum(string _id,
                                string _title,
                                string _content_url,
                                string _thumbnail_url,
                                string _published_date)
        {
            AlbumInfo oAlbumInfo = new AlbumInfo();
            oAlbumInfo.ID = _id;
            oAlbumInfo.Title = _title;
            oAlbumInfo.ContentURL = _content_url;
            oAlbumInfo.ThumbnailURL = _thumbnail_url;
            try
            {
                DateTime dtTemp = DateTime.Parse(_published_date);
                oAlbumInfo.PublishedDate = dtTemp.ToShortDateString();
            }
            catch (Exception){ /*Do nothing*/ }

            m_Albums.Add(oAlbumInfo);

            oAlbumInfo.AlbumCount = m_Albums.Count;
        }

        public void Clear()
        {
            m_Albums.Clear();
            m_CurrentSlides.Clear();
        }

        public AuthorInfo _AuthorInfo
        {
            get { return m_AuthorInfo; }
        }

        public IList<AlbumInfo> _Albums
        {
            get { return m_Albums; }
        }

        public PRI_Slides _CurrentSlides
        {
            get { return m_CurrentSlides; }
        }

        #region IAlbums Members


        public SSError _LastError
        {
            get { return m_SSError; }
            set { m_SSError = value; }
        }

        public void FireEvent_AlbumsReady()
        {
            if (null != Event_AlbumsReady)
            {
                Event_AlbumsReady();
            }
        }

        public void FireEvent_SlidesReady()
        {
            if (null != Event_SlidesReady)
            {
                Event_SlidesReady();
            }
        }


        #endregion
    }
}
